import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { UserLoginDTO } from './user.dto'
import * as dateFns from 'date-fns'
import { randString } from '../../tool/tool'
// import { Between } from 'typeorm';
// import * as crypto from "crypto";
// import * as xml2js from 'xml2js'
import * as db from '../../entity'

@Injectable()
export class UserService {
    // constructor(private readonly ) { }

    async userLogin(loginInfo: UserLoginDTO, type: db.UserTokenTypeEnum) {
        const codeInfo = await db.MobileSms.findOne({
            where: {
                mobile: loginInfo.data.phone,
                code: loginInfo.data.code,
                type: type,
                status: db.SmsCodeStatusEnum.ENABLE,
            },
            order: {
                create_time: 'ASC'
            }
        })
        if (!codeInfo) {
            throw new HttpException('短信验证码错误', HttpStatus.OK);
        }
        if (dateFns.isAfter(new Date(), codeInfo.expire_time)) {
            codeInfo.status = db.SmsCodeStatusEnum.DISABLE;
            codeInfo.save();
            throw new HttpException('验证码已经过期，请重新获取', HttpStatus.OK);
        }
        codeInfo.status = db.SmsCodeStatusEnum.DISABLE;
        await codeInfo.save();

        let userId = 0;
        if (type === db.UserTokenTypeEnum.USER) {
            const findUser = await db.UserAccount.findOne({
                where: {
                    mobile: loginInfo.data.phone
                }
            })
            if (!findUser) {
                const user = await db.UserAccount.create({
                    mobile: loginInfo.data.phone
                })
                await user.save();
                userId = user.user_id;
            } else {
                userId = findUser.user_id;
            }
            const token = randString(32);
            await db.UserToken.update(
                {
                    user_id: userId,
                    type: db.UserTokenTypeEnum.USER
                },
                {
                    status: db.UserTokenStatusEnum.DISABLE
                }
            )
            const userCredit = db.UserToken.create({
                user_id: userId,
                token: token,
                type: db.UserTokenTypeEnum.USER,
                expire_time: dateFns.addDays(new Date(), 3)
            })
            await userCredit.save();
            return { data: { token: token } }
        } else {
            const findUser = await db.CrediterAccount.findOne({
                where: {
                    mobile: loginInfo.data.phone
                }
            })
            if (!findUser) {
                const user = await db.CrediterAccount.create({
                    mobile: loginInfo.data.phone
                })
                await user.save();
                userId = user.crediter_id;
            } else {
                userId = findUser.crediter_id;
            }
            const token = randString(32);
            await db.UserToken.update(
                {
                    user_id: userId,
                    type: db.UserTokenTypeEnum.CREDITER
                },
                {
                    status: db.UserTokenStatusEnum.DISABLE
                })
            await db.UserToken.create({
                user_id: userId,
                token: token,
                type: db.UserTokenTypeEnum.CREDITER,
                expire_time: dateFns.addDays(new Date(), 7)
            }).save();
            return { data: { token: token } }
        }
    }
}